#coding=utf-8
from uliweb.orm import *
from uliweb.utils.common import get_var

def get_modified_user():
    from uliweb import request
    
    return request.user.id

#非开发类任务表06-22,只需要状态表
class Tasks(Model):
    __verbose_name__ = u'非开发任务'
    
    task_id = Field(CHAR, verbose_name='任务编号', max_length=18, required=True, unique=True, index=True)
    task_sys = Reference('system', verbose_name='所属系统')
    rel_sys = Reference('system', verbose_name='关联系统', collection_name='rel_sys_tasks')
    task_type = Field(CHAR, verbose_name='任务类型',max_length=2,choices=get_var('PARA/TASK_TYPE_NONDEVELOP'),required=True)
    name = Field(str, verbose_name='任务名称', max_length=255, required=True)
    tx = Reference('tx', verbose_name='条线')
    assignment = Reference('assignment', verbose_name='资源来源任务书', collection_name='ass_tasks')
    requirement = Reference('requirement', verbose_name='所属准开发需求', collection_name='req_tasks') 
    amount_inner = Field(float,verbose_name='中心工作量/预计投入')
    amount_outer = Field(float,verbose_name='公司工作量/预计投入')
    amount_inner_input = Field(float,verbose_name='中心工作量/已投入')
    amount_outer_input = Field(float,verbose_name='公司工作量/已投入')
    amount_inner_design = Field(float,verbose_name='中心工作量/设计投入')
    amount_outer_design = Field(float,verbose_name='公司工作量/设计投入')
    amount_inner_out8 = Field(float,verbose_name='中心工作量/8小时外投入')
    amount_outer_out8 = Field(float,verbose_name='公司工作量/8小时外投入')
    amount_inner_weekend = Field(float,verbose_name='中心工作量/周末投入')
    amount_outer_weekend = Field(float,verbose_name='公司工作量/周末投入')
    kfzx = Reference('kfzx', verbose_name='开发中心')
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    status = Field(CHAR, verbose_name='状态', max_length=1, required=True, choices=get_var('PARA/TASK_STATUS'), default='1')
    begin_time = Field(datetime.date, verbose_name='任务开始时间', required=True)
    end_time = Field(datetime.date, verbose_name='任务结束时间', required=True)
    memo = Field(TEXT, verbose_name='备注', hint="若该任务是DCM单任务，请在备注中填写DCM单号")    
    created_date = Field(datetime.datetime, verbose_name='创建时间', auto_now_add=True)
    desc = Field(TEXT, verbose_name='任务描述')
    stakeholder = Reference('user', verbose_name='任务责任人', collection_name='stakeholder_tasks')
    deleted = Field(bool, verbose_name='删除状态')
    
    class AddForm:
        fields = ['requirement', 'name', 'task_sys', 'assignment', 'amount_inner', 'task_type',
            'amount_outer', 'begin_time', 'end_time', 'stakeholder', 'memo', 'desc',
            ]
    
    class EditForm:
#        fields = ['requirement', 'name', 'task_sys', 'assignment', 'amount_inner', 'task_type',
#            'amount_outer', 'begin_time', 'end_time','memo', 'status',
#            'amount_inner_input', 'amount_outer_input', 
#            'amount_inner_design', 'amount_outer_design', 'amount_inner_out8', 
#            'amount_outer_out8', 'amount_inner_weekend', 'amount_outer_weekend',
#            ]
        fields = ['task_sys', 'name', 'assignment', 'amount_inner', 'task_type',
            'amount_outer', 'begin_time', 'end_time', 'stakeholder', 'memo', 'status', 'desc',
            ]
        

    class Table:
        fields = [
            {'name':'task_id', 'width':120},
            {'name':'name', 'width':200},
            {'name':'kfzx', 'width':60},
            {'name':'task_sys', 'width':50},
            {'name':'task_type', 'width':60},
            {'name':'stakeholder', 'width':80},
            {'name':'assignment', 'width':120},
            {'name':'requirement', 'width':120},
            {'name':'begin_time', 'width':80},
            {'name':'end_time', 'width':80},
            {'name':'status', 'width':60},
            {'name':'amount_inner', 'width':40},
            {'name':'amount_inner_input', 'width':40},
            {'name':'amount_inner_design', 'width':40},
            {'name':'amount_inner_out8', 'width':40},
            {'name':'amount_inner_weekend', 'width':40},
            {'name':'amount_outer', 'width':40},
            {'name':'amount_outer_input', 'width':40},
            {'name':'amount_outer_design', 'width':40},
            {'name':'amount_outer_out8', 'width':40},
            {'name':'amount_outer_weekend', 'width':40},
            {'name':'modified_date', 'width':120},
            {'name':'modified_user', 'width':60},
        ]
        
    def __unicode__(self):
        return "%s-%s" % (self.task_id, self.name)
    
    def get_url(self):
        return u'<a href="/tasks/nondevelop/%d" title="%s">%s</a>' % (self.id, self.name, self.name)
    
class Tasks_Details(Model):
    __verbose_name__ = u'任务明细'

    task_id = Field(CHAR, max_length=18, verbose_name='任务编号')
    requirement = Reference('requirement', verbose_name='所属准开发需求') 
    task_sys = Reference('system', verbose_name='所属系统')
    kfzx = Reference('kfzx', verbose_name='开发中心')
    tx = Reference('tx', verbose_name='条线')
    task_type = Field(CHAR, verbose_name='任务类型',max_length=2, choices=get_var('PARA/TASK_TYPE'))
    assignment = Reference('assignment', verbose_name='资源来源任务书')
    begin_time = Field(datetime.date, verbose_name='当前阶段开始日期', required=True)
    end_time = Field(datetime.date, verbose_name='当前阶段结束日期', required=True)
    amount_inner_input = Field(float,verbose_name='中心工作量/已投入')
    amount_outer_input = Field(float,verbose_name='公司工作量/已投入')
    amount_inner_design = Field(float,verbose_name='中心工作量/设计投入')
    amount_outer_design = Field(float,verbose_name='公司工作量/设计投入')
    amount_inner_out8 = Field(float,verbose_name='中心工作量/8小时外投入')
    amount_outer_out8 = Field(float,verbose_name='公司工作量/8小时外投入')
    amount_inner_weekend = Field(float,verbose_name='中心工作量/周末投入')
    amount_outer_weekend = Field(float,verbose_name='公司工作量/周末投入')
    stage = Field(CHAR, verbose_name='当前所处阶段',max_length=2,choices=get_var('PARA/TASK_STAGE'))
    year = Field(int, verbose_name='年', required=True)
    month = Field(int, verbose_name='月', required=True)
    year_month = Field(int, verbose_name='年月(yyyymm)', required=True)
    rel_sys = Reference('system', verbose_name='关联系统', collection_name='rel_sys_tasks_details')
    created_date = Field(datetime.datetime, verbose_name='创建时间', auto_now_add=True)
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now_add=True, auto_now=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    memo = Field(TEXT, verbose_name='备注')
    plan = Field(int, verbose_name='阶段ID')
    
    class Table:
        fields = [
            {'name':'task_id', 'width':200},
            {'name':'task_type', 'width':100},
            {'name':'begin_time', 'width':80},
            {'name':'end_time', 'width':80},
            {'name':'stage', 'width':60},
            {'name':'amount_inner_input', 'width':40},
            {'name':'amount_inner_design', 'width':40},
            {'name':'amount_inner_out8', 'width':40},
            {'name':'amount_inner_weekend', 'width':40},
            {'name':'amount_outer_input', 'width':40},
            {'name':'amount_outer_design', 'width':40},
            {'name':'amount_outer_out8', 'width':40},
            {'name':'amount_outer_weekend', 'width':40},
            {'name':'requirement', 'width':200},
            {'name':'assignment', 'width':200},
            {'name':'memo', 'width':120},
        ]
    @classmethod
    def OnInit(cls):
        Index('tasks_details_indx', cls.c.task_id, cls.c.stage, unique=False)
    
class Researches(Model):    
    __verbose_name__ = u'可研'
    
    task_id = Field(CHAR, verbose_name='任务编号', max_length=18, required=True, unique=True, index=True)
    task_sys = Reference('system', verbose_name='所属系统', required=True)
    task_type = Field(CHAR, verbose_name='任务类型',max_length=2,default='05')
    tx = Reference('tx', verbose_name='条线')
    name = Field(str, verbose_name='开发任务名称', max_length=255, required=True)
    assignment = Reference('assignment', verbose_name='资源来源任务书', collection_name='ass_researches')
    requirement = Reference('requirement', verbose_name='所属准开发需求', required=True, collection_name='req_researches') 
    amount_inner = Field(float,verbose_name='中心工作量/预计投入')
    amount_outer = Field(float,verbose_name='公司工作量/预计投入')
    amount_inner_input = Field(float,verbose_name='中心工作量/已投入')
    amount_outer_input = Field(float,verbose_name='公司工作量/已投入')
    amount_inner_design = Field(float,verbose_name='中心工作量/设计投入')
    amount_outer_design = Field(float,verbose_name='公司工作量/设计投入')
    amount_inner_out8 = Field(float,verbose_name='中心工作量/8小时外投入')
    amount_outer_out8 = Field(float,verbose_name='公司工作量/8小时外投入')
    amount_inner_weekend = Field(float,verbose_name='中心工作量/周末投入')
    amount_outer_weekend = Field(float,verbose_name='公司工作量/周末投入')
    kfzx = Reference('kfzx', verbose_name='开发中心')
    rel_sys = Reference('system', verbose_name='关联系统', collection_name='rel_sys_researches')
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    status = Field(CHAR, verbose_name='状态', max_length=1, required=True, choices=get_var('PARA/RESEARCH_STATUS'), default='1')
    begin_time = Field(datetime.date, verbose_name='任务开始时间', required=True)
    end_time = Field(datetime.date, verbose_name='任务结束时间', required=True)
    memo = Field(TEXT, verbose_name='备注')    
    created_date = Field(datetime.datetime, verbose_name='创建时间', auto_now_add=True)
    desc = Field(TEXT, verbose_name='任务描述')
    stakeholder = Reference('user', verbose_name='任务责任人', collection_name='stakeholder_research_tasks')
    deleted = Field(bool, verbose_name='删除状态')
    
    def __unicode__(self):
        return "%s-%s" % (self.task_id, self.name)

    class AddForm:
        fields = ['requirement', 'name', 'task_sys', 'assignment', 'amount_inner',
            'amount_outer', 'begin_time', 'end_time', 'stakeholder', 'memo', 'desc',
            ]
            
    class EditForm:
#        fields = ['requirement', 'name', 'task_sys', 'assignment', 'amount_inner',
#            'amount_outer', 'begin_time', 'end_time','memo', 'status',
#            'amount_inner_input', 'amount_outer_input', 
#            'amount_inner_design', 'amount_outer_design', 'amount_inner_out8', 
#            'amount_outer_out8', 'amount_inner_weekend', 'amount_outer_weekend',
#            ]
        fields = ['name', 'assignment', 'amount_inner',
            'amount_outer', 'begin_time', 'end_time', 'stakeholder', 'memo', 'status', 'desc',
            ]
        
    
    class Table:
        fields = [
            {'name':'task_id', 'width':120},
            {'name':'name', 'width':200},
            {'name':'kfzx', 'width':40},
            {'name':'task_sys', 'width':60},
            {'name':'task_type', 'width':60},
            {'name':'stakeholder', 'width':80},
            {'name':'assignment', 'width':120},
            {'name':'requirement', 'width':120},
            {'name':'begin_time', 'width':80},
            {'name':'end_time', 'width':80},
            {'name':'status', 'width':60},
            {'name':'amount_inner', 'width':40},
            {'name':'amount_inner_input', 'width':40},
            {'name':'amount_inner_design', 'width':40},
            {'name':'amount_inner_out8', 'width':40},
            {'name':'amount_inner_weekend', 'width':40},
            {'name':'amount_outer', 'width':40},
            {'name':'amount_outer_input', 'width':40},
            {'name':'amount_outer_design', 'width':40},
            {'name':'amount_outer_out8', 'width':40},
            {'name':'amount_outer_weekend', 'width':40},
            {'name':'modified_date', 'width':120},
            {'name':'modified_user', 'width':60},
        ]
        
    def get_url(self):
        return u'<a href="/tasks/researches/%d" title="%s">%s</a>' % (self.id, self.name, self.name)
    
#开发类任务表01-04,状态表（05可研类工作量建议在requirement里面体现）
class Tasks_Develop(Model):    
    __verbose_name__ = u'开发任务'
    
    task_id = Field(CHAR, verbose_name='任务编号', max_length=18, required=True, unique=True, index=True)
    name = Field(str, verbose_name='开发任务名称', max_length=255, required=True)
    task_sys = Reference('system', verbose_name='所属系统', required=True, collection_name='sys_tasks_develop')
    tx = Reference('tx', verbose_name='条线')
    task_type = Field(CHAR, verbose_name='任务类型',max_length=2,required=True, choices=get_var('PARA/TASK_TYPE_DEVELOP'))
    assignment = Reference('assignment', verbose_name='资源来源任务书', collection_name='ass_tasks_develop')
    amount_inner = Field(float,verbose_name='中心工作量/预计投入')
    amount_outer = Field(float,verbose_name='公司工作量/预计投入')
    amount_inner_input = Field(float,verbose_name='中心工作量/已投入')
    amount_outer_input = Field(float,verbose_name='公司工作量/已投入')
    amount_inner_design = Field(float,verbose_name='中心工作量/设计投入')
    amount_outer_design = Field(float,verbose_name='公司工作量/设计投入')
    amount_inner_out8 = Field(float,verbose_name='中心工作量/8小时外投入')
    amount_outer_out8 = Field(float,verbose_name='公司工作量/8小时外投入')
    amount_inner_weekend = Field(float,verbose_name='中心工作量/周末投入')
    amount_outer_weekend = Field(float,verbose_name='公司工作量/周末投入')
    requirement = Reference('requirement', verbose_name='所属准开发需求', required=True, collection_name='req_tasks_develop') 
    begin_time = Field(datetime.date, verbose_name='任务开始时间', required=True)
    end_time = Field(datetime.date, verbose_name='任务结束时间', required=True)
    stage = Field(CHAR, verbose_name='所处阶段',max_length=2,required=True, choices=get_var('PARA/TASK_STAGE'), default='00')
    stage_enterTime = Field(datetime.date, verbose_name='阶段进入时间')
    #阶段结束时间，上一阶段在明细表中体现
    test_level = Field(CHAR, verbose_name='测试级别',max_length=2,choices=get_var('PARA/TASK_TEST_MODE'))
    test_beginDate = Field(datetime.date, verbose_name='集成测试计划开始时间')
    test_endDate = Field(datetime.date, verbose_name='集成测试计划结束时间')
    version_date = Field(datetime.date, verbose_name='计划投产时间')
    #其它里程碑点
    req_close_date = Field(datetime.date, verbose_name='需求封版计划时间')
    design_close_date = Field(datetime.date, verbose_name='设计封版计划时间')
    uat_begin_date = Field(datetime.date, verbose_name='UAT测试计划开始时间')
    uat_end_date = Field(datetime.date, verbose_name='UAT测试计划结束时间')
    tune_begin_date = Field(datetime.date, verbose_name='性能调优计划开始时间')
    tune_end_date = Field(datetime.date, verbose_name='性能调优计划结束时间')
    func_inst_begin_date = Field(datetime.date, verbose_name='功能版本安装测试计划开始时间')
    func_inst_end_date = Field(datetime.date, verbose_name='功能版本安装测试计划结束时间')
    tune_inst_begin_date = Field(datetime.date, verbose_name='非功能版本安装测试计划开始时间')
    tune_inst_end_date = Field(datetime.date, verbose_name='非功能版本安装测试计划结束时间')
    rel_sys = Reference('system', verbose_name='关联系统', collection_name='rel_sys_tasks_develop')
    version_headerId = SelfReference(verbose_name='主任务ID', collection_name='child_tasks')
    status = Field(CHAR, max_length=1, verbose_name='任务执行情况', required=True, choices=get_var('PARA/TASK_STATUS'), default='1')
    delay_classes = Field(CHAR, max_length=2, verbose_name='延迟原因分类', choices=get_var('PARA/TASK_DELAY_CLASSES'))
    delay_reason = Field(TEXT, verbose_name='延迟原因说明')
    risk_if = Field(bool, verbose_name='是否存在风险')
    risk_reason = Field(TEXT, verbose_name='风险说明')
    memo = Field(TEXT, verbose_name='备注')    
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    kfzx = Reference('kfzx', verbose_name='开发中心')
    test_plan = Field(FILE, verbose_name='测试方案')
    created_date = Field(datetime.datetime, verbose_name='创建时间', auto_now_add=True)
    sub_sys = Field(str, max_length=20, verbose_name='子系统英文简称')
    sub_sys_name = Field(str, max_length=200, verbose_name='子系统中文名称')
    desc = Field(TEXT, verbose_name='任务描述')
    selfdev_if = Field(bool, verbose_name='是否参与系统自主率考核', choices=get_var('PARA/YESNO'),default=True)
    stakeholder = Reference('user', verbose_name='任务责任人', collection_name='stakeholder_develop_tasks')
    deleted = Field(bool, verbose_name='删除状态')
    
    def __unicode__(self):
        return "%s-%s" % (self.task_id, self.name)

    class AddForm:
        fields = ['version_headerId','name', 'task_sys', 'sub_sys', 'sub_sys_name', 'task_type', 'assignment','requirement', 
            'amount_inner','amount_outer', 'begin_time', 'end_time', 'stakeholder',
            'memo', 'desc',
#            'delay_classes', 'delay_reason', 'risk_if', 'risk_reason',
            ]
#        layout = [
#            '-- 任务信息 --',
#            'task_sys', 'requirement', 'assignment', 'version_headerId',
#            'task_type', 'name', 
#            ('sub_sys', 'sub_sys_name'),
#            'desc',
#            '-- 预计工作量（单位：人天） --',
#            ('amount_inner','amount_outer'),
#            '-- 任务周期 --',
#            ('begin_time', 'end_time'),
#            '-- 备注 --',
#            'memo'
#        ]
            
    class EditForm:
#        fields = ['version_headerId','name', 'task_sys', 'task_type', 'assignment','requirement', 
#            'amount_inner','amount_outer', 'begin_time', 'end_time', 'test_level', 
#            'test_beginDate', 'test_endDate', 'version_date',
#            'stage', 'stage_enterTime', 'memo', 'test_plan', 'status',
#            'amount_inner_input', 'amount_outer_input', 
#            'amount_inner_design', 'amount_outer_design', 'amount_inner_out8', 
#            'amount_outer_out8', 'amount_inner_weekend', 'amount_outer_weekend',
#            'delay_classes', 'delay_reason', 'risk_if', 'risk_reason',
#            'req_close_date', 'design_close_date', 'uat_begin_date',
#            'uat_end_date', 'tune_begin_date', 'tune_end_date',
#            'func_inst_begin_date', 'func_inst_end_date', 'tune_inst_begin_date',
#            'tune_inst_end_date',
#        ]
        fields = ['name', 'sub_sys', 'sub_sys_name', 'task_type', 'assignment', 
            'amount_inner','amount_outer', 'begin_time', 'end_time',  'stakeholder',
            'memo', 'status', 'desc',
#            'delay_classes', 'delay_reason', 'risk_if', 'risk_reason',
        ]
        
        
    class DetailView:
        fields = ['task_id', 'name', 'kfzx', 'task_sys', 'sub_sys', 'sub_sys_name', 'task_type', 'assignment',
            'requirement','amount_inner','amount_outer',
            'amount_inner_input', 'amount_outer_input', 'amount_inner_design',
            'amount_outer_design', 'amount_inner_out8', 'amount_outer_out8',
            'amount_inner_weekend', 'amount_outer_weekend',
            'begin_time', 'end_time','stage', 'stage_enterTime',
            'version_headerId','status', 'version_date',
#            'delay_classes', 'delay_reason', 'risk_if', 'risk_reason',
            'stakeholder',
            'memo', 'modified_user', 'modified_date', 'desc','selfdev_if',
            ]
            
    class Table:
        fields = [
            {'name':'is_mine', 'verbose_name':'我的', 'width':40},
            {'name':'task_id', 'width':110},
            {'name':'version_headerId', 'width':120, 'verbose_name':'主任务'},
            {'name':'name', 'width':200},
            {'name':'kfzx', 'width':60},
            {'name':'tx', 'width':60},
            {'name':'task_sys', 'width':60},
            {'name':'sub_sys', 'width':40},
            {'name':'task_type', 'width':60},
            {'name':'stakeholder', 'width':80},
            {'name':'stage', 'width':60},
            {'name':'status', 'width':60},
            {'name':'requirement', 'width':120},
            {'name':'assignment', 'width':120},
            {'name':'begin_time', 'width':80},
            {'name':'end_time', 'width':80},
            {'name':'stage_enterTime', 'width':80},
            {'name':'version_date', 'width':80},
            {'name':'req_close_date', 'width':80},
            {'name':'req_finish_end_date','verbose_name':'需求封版时间','width':80},
            {'name':'design_close_date', 'width':80},
            {'name':'design_finish_end_date','verbose_name':'设计封版时间','width':80},            
            {'name':'test_version', 'verbose_name':'测试版本', 'width':60},
            {'name':'change_num', 'verbose_name':'测试案例变更次数'},
            {'name':'test_finish_rate', 'verbose_name':'测试案例通过比例'},
            {'name':'req_modified_date', 'verbose_name':'测试功能最晚变动时间'},
            {'name':'test_beginDate', 'width':120},
            {'name':'test_finish_beginDate','verbose_name':'集成测试实际开始时间', 'width':120},
            {'name':'test_endDate', 'width':120},
            {'name':'test_finish_endDate','verbose_name':'集成测试实际完成时间', 'width':120},
            {'name':'uat_begin_date', 'width':120},
            {'name':'uat_finish_begin_date','verbose_name':'用户测试实际开始时间', 'width':120},
            {'name':'uat_end_date', 'width':120},
            {'name':'uat_finish_end_date','verbose_name':'用户测试实际完成时间', 'width':120},
            {'name':'func_inst_begin_date', 'width':150},
            {'name':'func_inst_finish_begin_date','verbose_name':'功能版本安装测试实际开始时间', 'width':150},
            {'name':'func_inst_end_date', 'width':150},
            {'name':'func_inst_finish_end_date','verbose_name':'功能版本安装测试实际完成时间', 'width':150},
            {'name':'tune_inst_begin_date', 'width':150},
            {'name':'tune_inst_finish_begin_date','verbose_name':'非功能版本安装测试实际开始时间', 'width':150},
            {'name':'tune_inst_end_date', 'width':150},
            {'name':'tune_inst_finish_end_date','verbose_name':'非功能版本安装测试实际完成时间', 'width':150},
            {'name':'rel_sys', 'width':150},
            {'name':'amount_inner', 'width':40},
            {'name':'amount_inner_input', 'width':40},
            {'name':'amount_inner_design', 'width':40},
            {'name':'amount_inner_out8', 'width':40},
            {'name':'amount_inner_weekend', 'width':40},
            {'name':'amount_outer', 'width':40},
            {'name':'amount_outer_input', 'width':40},
            {'name':'amount_outer_design', 'width':40},
            {'name':'amount_outer_out8', 'width':40},
            {'name':'amount_outer_weekend', 'width':40},
#            {'name':'delay_classes'},
#            {'name':'delay_reason'},
#            {'name':'risk_if', 'width':80},
#            {'name':'risk_reason'},
            {'name':'selfdev_if', 'width':40},
            {'name':'created_date', 'width':120},
            {'name':'modified_date', 'width':120},
            {'name':'modified_user', 'width':40},
#            {'name':'sub_sys_name', 'width':40},
            {'name':'memo', 'width':100},
        ]
        
    def get_url(self):
        return u'<a href="/tasks/develop/%d" title="%s">%s</a>' % (self.id, self.name, self.name)

    def get_receiver_ids(self, sender_id):
        if not self.version_headerId:
            return self.requirement.get_receiver_ids(sender_id)
        else: 
            User = get_model('user')
            test_user = User.filter(User.c.department =='3')
            users = set(self.task_sys.users.ids())
            if sender_id in users:
                users.remove(sender_id)
            for row in test_user:
                if row.id in users:
                    users.remove(row.id)
            return list(users)
    
class MileStone_Plan(Model):    
    __verbose_name__ = u'里程碑计划'

    task = Reference('tasks_develop', verbose_name="开发任务", collection_name='milestones', index=True)
    name = Field(str, verbose_name="名称", max_length=40, required=True)
    plan_date = Field(datetime.date, verbose_name="计划完成时间")
    status = Field(CHAR, verbose_name="执行状态", max_length=1, required=True, default='1', choices=get_var('PARA/MILESTONE_STATUS'))
    check = Field(CHAR, verbose_name="检查", max_length=1, choices=get_var('PARA/MILESTONE_CHECK'))
    flag = Field(CHAR, verbose_name="保留字段", max_length=1) #'B' SIT测试开始， 'E' SIT结束 'S' 投产
    memo = Field(TEXT, verbose_name="备注")
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user)
    
    class AddForm:
        fields = ('name', 'plan_date', 'memo')
        
    class EditForm:
        fields = ('name', 'plan_date', 'memo')
        
    def delete(self):
        for r in self.plan_milestones.all():
            r.delete()
        super(MileStone_Plan, self).delete()
        
class Tasks_MileStone(Model):    
    __verbose_name__ = u'任务执行里程碑'

    task = Reference('tasks_develop', verbose_name="开发任务", collection_name='tasks_milestones')
    milestone_plan = Reference('milestone_plan', verbose_name="里程碑", collection_name="plan_milestones")
    finish_date = Field(datetime.date, verbose_name="实际完成时间")
    status = Field(CHAR, verbose_name="执行状态", max_length=1, required=True, default='1', choices=get_var('PARA/MILESTONE_STATUS'))
    memo = Field(TEXT, verbose_name="备注")
    submission = Field(FILE, verbose_name='提交件')
    check = Field(CHAR, verbose_name="检查", max_length=1, choices=get_var('PARA/MILESTONE_CHECK'))
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user)
    
    class EditForm:
        fields = ('finish_date', 'memo')
        
    @classmethod
    def OnInit(cls):
        Index('tasks_milestone_indx', cls.c.task, cls.c.milestone_plan, unique=False)
    
        
class MileStone_Details(Model):    
    __verbose_name__ = u'里程碑明细'

    task = Reference('tasks_develop', verbose_name="开发任务", collection_name='tasks_milestone_detailss', required=True)
    milestone_plan = Reference('milestone_plan', verbose_name="里程碑计划", required=True)
    changed_value = Field(BLOB, verbose_name="修改内容")
    old_value = Field(BLOB, verbose_name="原内容")
    modify_flag = Field(CHAR, max_length=1, verbose_name="修改标志", choices=get_var('PARA/MILESTONE_MODIFY_FLAG'))
    modified_date = Field(datetime.datetime, verbose_name='修改时间')
    modified_user = Reference('user', verbose_name='修改人', collection_name='modifier_milestone_details')
    
def post_save(sender, instance, created, data, old_data):
    from uliweb.orm import get_model
    import pickle
    from datetime import datetime
    from uliweb import request

    if sender.tablename == 'milestone_plan':
        changed_value = pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
        if created:
            modify_flag = '1'
            old_value = ''
        else:
            old = {}
            modify_flag = '4'
            for k in data.iterkeys():
                old[k] = old_data[k]
            old_value = pickle.dumps(old, pickle.HIGHEST_PROTOCOL)
            if 'plan_date' in data:
                modify_flag = '2'
                
        obj = MileStone_Details(task=instance._task_, milestone_plan=instance, changed_value=changed_value,
            old_value=old_value, modified_date=datetime.now(), modified_user=request.user,
            modify_flag=modify_flag)
            
        obj.save()
    
def pre_delete(sender, instance):
    import pickle
    from datetime import datetime
    from uliweb import request
    
    if sender.tablename == 'milestone_plan':
        changed_value = ''
        old_value = pickle.dumps(instance.to_dict(), pickle.HIGHEST_PROTOCOL)
        modify_flag = '3'
            
        obj = MileStone_Details(task=instance._task_, milestone_plan=instance, changed_value=changed_value,
            old_value=old_value, modified_date=datetime.now(), modified_user=request.user,
            modify_flag=modify_flag)
            
        obj.save()
    
#任务对应功能点
class Task_Functions(Model):
    __verbose_name__ = u'任务功能点'
    
    task = Reference('tasks_develop', verbose_name='对应任务', required=True, collection_name='task_functions')
    function = Reference('sysfunction', verbose_name='系统功能点', required=True)
    change_status = Field(CHAR, verbose_name='变动状态', max_length=1, choices=get_var('PARA/CHANGE_STATUS'), required=True)
    open_status = Field(CHAR, max_length=1, verbose_name='开放使用状态', choices=get_var('PARA/YES_NO'))
    open_secverify = Field(CHAR, max_length=1, verbose_name='开放验密需求', choices=get_var('PARA/YES_NO'))
    test_point = Field(str, max_length=255, verbose_name='功能测试要点')
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    
    #以下为系统来修改
    is_executed = Field(CHAR, max_length=1, verbose_name='是否执行测试', choices=get_var('PARA/YES_NO'))
    test_result = Field(CHAR, max_length=1, verbose_name='功能测试结果', choices=get_var('PARA/YES_NO'))
    test_finish_date = Field(datetime.datetime, verbose_name='测试完成时间')
    
    @classmethod
    def OnInit(cls):
        Index('tasks_funcs_indx', cls.c.task, cls.c.function)
    
    def __unicode__(self):
        return unicode(self.function)
    
    class Table:
        fields = [
            {'name':'category', 'verbose_name':'分类', 'width':60},
            {'name':'function_code', 'width':100, 'verbose_name':'功能代码', 'sortable':True},
            {'name':'function', 'width':150, 'sortable':True},
            {'name':'change_status', 'width':60, 'sortable':True},
            {'name':'open_status', 'width':80, 'sortable':True},
            {'name':'open_secverify', 'width':80, 'sortable':True},
            {'name':'test_point', 'width':80, 'sortable':True},
            {'name':'is_executed', 'width':80, 'sortable':True},
            {'name':'test_result', 'width':80, 'sortable':True},
            {'name':'test_finish_date', 'width':80, 'sortable':True},
        ]
        
    class EditForm:
        fields = [
            'change_status',
            'open_status',
            'open_secverify',
            'test_point',
        ]